package webapp.mvc.controllers;

import com.zoowii.jpa_utils.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.RedirectView;
import webapp.mvc.components.HttpManager;
import webapp.mvc.controllers.forms.SimpleResponse;
import webapp.mvc.exceptions.CategoryException;
import webapp.mvc.exceptions.RedirectException;
import webapp.mvc.models.ShopCategoryEntity;
import webapp.mvc.services.ICategoryService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/category")
public class CategoryController {
    private static final Logger LOG = LoggerFactory.getLogger(CategoryController.class);
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private HttpManager httpManager;
    
    @RequestMapping("/shop_categories")
    public @ResponseBody
    Object listShopCategories(HttpServletRequest request, HttpServletResponse response) {
        try {
            httpManager.autoLoginOfWechat(request);
        } catch (RedirectException e) {
            return new RedirectView(e.getUrl());
        }
        return SimpleResponse.ok(categoryService.getShopCategories()).toJSON();
    }

    @RequestMapping(value = "/create_shop_category", method = RequestMethod.POST)
    public @ResponseBody
    String createShopCategory(String name, String logoUrl) {
        if(StringUtil.isEmpty(name)) {
            return SimpleResponse.fail("商户分类名称不可为空").toJSON();
        }
        try {
            ShopCategoryEntity shopCategoryEntity = categoryService.createShopCategory(name, logoUrl);
            return SimpleResponse.ok(shopCategoryEntity).toJSON();
        } catch (CategoryException e) {
            return SimpleResponse.fail(e.getMessage()).toJSON();
        }
    }

    @RequestMapping(value = "/delete_shop_category", method = RequestMethod.POST)
    public @ResponseBody
    String deleteShopCategory(String categoryIds) {
        String[] categoryIdArray = categoryIds.split(",");
        for(String categoryId : categoryIdArray) {
            ShopCategoryEntity shopCategoryEntity = categoryService.findById(categoryId);
            if(shopCategoryEntity != null) {
                try {
                    categoryService.deleteShopCategory(shopCategoryEntity);
                } catch (CategoryException e) {
                    return SimpleResponse.fail(e.getMessage()).toJSON();
                }
            }
        }
        return SimpleResponse.ok("删除商户分类成功").toJSON();
    }
    
    @RequestMapping("/shop_category/{id}")
    public @ResponseBody String getShopCategoryById(@PathVariable String id) {
        ShopCategoryEntity shopCategoryEntity = categoryService.findById(id);
        if(shopCategoryEntity!=null) {
            return SimpleResponse.ok(shopCategoryEntity).toJSON();
        } else {
            return SimpleResponse.fail("找不到商户分类" + id).toJSON();
        }
    }
}
